package com.dd.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dd.entity.DdOrder;
import com.dd.entity.DdShop;
import com.dd.entity.DdShopgoods;
import com.dd.entity.DdShopreject;
import com.dd.entity.DdSms;
import com.dd.entity.DdSystemuser;
import com.dd.entity.DdUser;
import com.dd.entity.DdUserorder;
import com.dd.entity.DdUserorder1;
import com.dd.entity.DdUserpay;
import com.dd.util.DateStrFormat;
import com.dd.util.PageList;

public class OrderSupportAction extends BaseAction {

	/**
	 * 查询订单--根据商家汇总订单量
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward queryordershop(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		String logid = String.valueOf((Integer) request.getSession()
				.getAttribute("systemlogid"));// 系统用户是否已经登录

		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		if (logid == null || logid.trim().length() == 0) {// 商家用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
			return null;
		}
		try {
			String stratdate = request.getParameter("stratdate");// 订单确认的开始时间
			String enddate = request.getParameter("enddate");// 订单确认的结束时间
			String shopid = request.getParameter("shopid");// 商家id
			StringBuffer sqlwhere = new StringBuffer();

			// 确认时间
			if (stratdate != null && stratdate.trim().length() > 0) {
				sqlwhere.append(" and a.paydate>='" + stratdate + "'");
			}
			if (enddate != null && enddate.trim().length() > 0) {
				sqlwhere.append(" and a.paydate<='" + enddate + "'");
			}
			// 成功的订单才算
			sqlwhere.append(" and a.status in (2,3)");
			// 按商家
			if (shopid != null && shopid.trim().length() > 0) {
				sqlwhere.append(" and a.shopid=" + shopid + " ");
			}
			List list = this.getDdOrderDAO().findAllGroupByShop(
					sqlwhere.toString());
			for (int i = 0; i < list.size(); i++) {
				List temli = (List) list.get(i);
				String types = (String) temli.get(0);
				Integer num = (Integer) temli.get(1);
				Float amount = (Float) temli.get(2);
				String typename = "";
				if (types.equals("1")) {
					typename = "餐厅";
				} else if (types.equals("2")) {
					typename = "娱乐";
				} else if (types.equals("3")) {
					typename = "酒店";
				} else if (types.equals("4")) {
					typename = "商城";
				} else if (types.equals("5")) {
					typename = "运动";
				}
				sbf.append("<大类  商家类型=\"" + typename + "\" 订单量=\"" + num
						+ "\" 总金额=\"" + amount + "\" >");
				List list_detail = this.getDdOrderDAO().findDetailGroupByShop(
						sqlwhere.toString(), types);
				for (int j = 0; j < list_detail.size(); j++) {
					temli = (List) list_detail.get(j);
					String name = (String) temli.get(0);
					num = (Integer) temli.get(1);
					amount = (Float) temli.get(2);
					sbf.append("<商家明细  商家名=\"" + name + "\" 订单量=\"" + num
							+ "\" 总金额=\"" + amount + "\" />");
				}
				sbf.append("</大类>");
			}
			sbf.append("</root>");
			out.print(sbf.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return null;
	}

	/**
	 * 查询订单--根据业务员汇总订单量
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward queryorderyw(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		String logid = String.valueOf((Integer) request.getSession()
				.getAttribute("systemlogid"));// 系统用户是否已经登录

		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		if (logid == null || logid.trim().length() == 0) {// 商家用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
			return null;
		}
		try {
			String stratdate = request.getParameter("stratdate");// 订单确认的开始时间
			String enddate = request.getParameter("enddate");// 订单确认的结束时间
			String ulogid = request.getParameter("logid");// 业务员登录id
			StringBuffer sqlwhere = new StringBuffer();

			// 确认时间
			if (stratdate != null && stratdate.trim().length() > 0) {
				sqlwhere.append(" and a.confirmdate>='" + stratdate + "'");
			}
			if (enddate != null && enddate.trim().length() > 0) {
				sqlwhere.append(" and a.confirmdate<='" + enddate + "'");
			}
			// 成功的订单才算
			sqlwhere.append(" and a.status=3");
			// 按业务员
			if (ulogid != null && ulogid.trim().length() > 0) {
				if (!ulogid.equals("0")) {
					sqlwhere.append(" and b.belong=" + ulogid + " ");
				}

			}
			List list = this.getDdOrderDAO().findAllGroupByYwMain(
					sqlwhere.toString());
			for (int k = 0; k < list.size(); k++) {
				List yeli = (List) list.get(k);
				Integer belong = (Integer) yeli.get(0);
				Integer num = (Integer) yeli.get(1);
				Float amount = (Float) yeli.get(2);
				DdSystemuser dsu = this.getDdSystemuserDAO().findById(belong);

				sbf.append("<业务员  姓名=\"" + dsu.getName() + "\" 订单量=\"" + num
						+ "\" 总金额=\"" + amount + "\" >");
				yeli = this.getDdOrderDAO().findAllGroupByYw(
						sqlwhere.toString(), belong + "");
				for (int i = 0; i < yeli.size(); i++) {
					List temli = (List) yeli.get(i);
					String types = (String) temli.get(0);
					num = (Integer) temli.get(1);
					amount = (Float) temli.get(2);
					String typename = "";
					if (types.equals("1")) {
						typename = "餐厅";
					} else if (types.equals("2")) {
						typename = "娱乐";
					} else if (types.equals("3")) {
						typename = "酒店";
					} else if (types.equals("4")) {
						typename = "商城";
					} else if (types.equals("5")) {
						typename = "运动";
					}
					sbf.append("<大类  商家类型=\"" + typename + "\" 订单量=\"" + num
							+ "\" 总金额=\"" + amount + "\" >");
					List list_detail = this.getDdOrderDAO()
							.findDetailGroupByYw(sqlwhere.toString(), types);
					for (int j = 0; j < list_detail.size(); j++) {
						temli = (List) list_detail.get(j);
						String name = (String) temli.get(0);
						num = (Integer) temli.get(1);
						amount = (Float) temli.get(2);
						sbf.append("<商家明细  商家名=\"" + name + "\" 订单量=\"" + num
								+ "\" 总金额=\"" + amount + "\" />");
					}
					sbf.append("</大类>");
				}
				sbf.append("</业务员 >");

			}
			sbf.append("</root>");
		//	System.out.println(sbf.toString());
			out.print(sbf.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return null;
	}

	/**
	 * 查询订单--后台报表
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward systemqueryorder2(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		String logid = String.valueOf((Integer) request.getSession()
				.getAttribute("systemlogid"));// 系统用户是否已经登录

		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		if (logid == null || logid.trim().length() == 0) {// 商家用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
			return null;
		}
		try {
			String stratdate = request.getParameter("stratdate");// 订单创建的开始时间
			String enddate = request.getParameter("enddate");// 订单创建的结束时间
			String pageNo = request.getParameter("pageno");// 第几页
			String pageSize = request.getParameter("pagesize");// 每页记录数
			String status = request.getParameter("status");// 订单状态
			String shopname = request.getParameter("shopname");// 商家名称
			String orderid = request.getParameter("orderid");// 订单号
			String userid = request.getParameter("userid");// 业务员id
			String ulogid = request.getParameter("logid");// 用户登录
			StringBuffer sqlwhere = new StringBuffer();
			if (stratdate != null && stratdate.trim().length() > 0) {
				sqlwhere.append(" and a.orderdate>='" + stratdate + "'");
			}
			if (enddate != null && enddate.trim().length() > 0) {
				sqlwhere.append(" and a.orderdate<='" + enddate + "'");
			}
			if (status != null && status.trim().length() > 0) {
				sqlwhere.append(" and a.status in(" + status.trim() + ")");

			}
			if (shopname != null && shopname.trim().length() > 0) {
				sqlwhere.append(" and b.name like '%" + shopname + "%'");
			}
			if (userid != null && userid.trim().length() > 0) {
				sqlwhere.append(" and b.belong =" + userid);
			}
			if (ulogid != null && ulogid.trim().length() > 0) {
				DdUser du = this.getDdUserDAO().findBylogid(ulogid);
				if (du == null) {
					ulogid = "0";
				} else {
					ulogid = du.getId() + "";
				}
				sqlwhere.append(" and a.userid =" + ulogid);
			}
			if (orderid != null && orderid.trim().length() > 0) {
				long _orderid = 0;
				try {
					_orderid = Long.parseLong(orderid);
					_orderid = _orderid - 100000000;
				} catch (Exception e) {
					_orderid = 0;
				}
				sqlwhere.append(" and a.id =" + _orderid);

			}
			// if (shopname != null && shopname.trim().length() > 0) {
			// sqlwhere.append(" and b.name>='" + shopname + "'");
			// }
			// sqlwhere.append(" and a.shopid=" + logid);
			int i_pageNo = 1;
			if (pageNo != null && pageNo.trim().length() > 0) {
				i_pageNo = Integer.parseInt(pageNo);
			} else {
				i_pageNo = 1;
			}
			int i_pageSize = 10;
			if (pageSize != null && pageSize.trim().length() > 0) {
				i_pageSize = Integer.parseInt(pageSize);
			} else {
				i_pageSize = 10;
			}
			PageList list = this.getDdOrderDAO().findAll(sqlwhere.toString(),
					i_pageNo, i_pageSize);
			sbf.append("<ordershops total=\"" + list.getTotalCount() + "\">");
			for (int i = 0; i < list.size(); i++) {
				Object[] obj = (Object[]) list.get(i);
				DdOrder du = (DdOrder) obj[0];
				DdShop ds = (DdShop) obj[1];
				String date2 = "";
				if (du.getType().intValue() == 2) {
					// 定位
					DdUserorder1 duo = (DdUserorder1) this.getDdUserorder1DAO()
							.findByOrderid(du.getId()).get(0);
					date2 = DateStrFormat.getCurrentTime(duo.getOrderdate1());
				} else {
					DdUserorder duo = (DdUserorder) this.getDdUserorderDAO()
							.findByOrderid(du.getId()).get(0);
					date2 = duo.getSenddate();
				}
				
				sbf.append("<ordershop shopid=\"" + ds.getId()
						+ "\" shopname=\"" + ds.getName() + "\" shoptype=\""
						+ ds.getType() + "\" id=\"" + du.getId()
						+ "\"  orderid=\"" + (du.getId() + 100000000)
						+ "\"  orderdate=\""
						+ DateStrFormat.getCurrentTime(du.getOrderdate())
						+ " \" orderdate2=\"" + date2 + "\" orderstatus=\""
						+ du.getStatus() + " \" orderstatusstr=\""
						+ getOrderstatus(du.getStatus()) + " \"  type=\""
						+ du.getType() + "\" typestr=\""
						+ getOrderType(du.getType().intValue()) + "\"/>");
			}
			sbf.append("</ordershops></root>");
			out.print(sbf.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return null;
	}

	/**
	 * 查询订单--商家报表
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward queryorder2(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		Integer logid = (Integer) request.getSession()
				.getAttribute("shoplogid");// 网站用户是否已经登录
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		if (logid == null) {// 商家用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
			return null;
		}
		try {
			String stratdate = request.getParameter("stratdate");// 订单确认的开始时间
			String enddate = request.getParameter("enddate");// 订单确认的结束时间
			String pageNo = request.getParameter("pageno");// 第几页
			String pageSize = request.getParameter("pagesize");// 每页记录数
			StringBuffer sqlwhere = new StringBuffer();
			StringBuffer sqlwhere2 = new StringBuffer();
			if (stratdate != null && stratdate.trim().length() > 0) {
				sqlwhere.append(" and a.confirmdate>='" + stratdate + "'");
				sqlwhere2.append(" and confirmdate>='" + stratdate + "'");
			}
			if (enddate != null && enddate.trim().length() > 0) {
				sqlwhere.append(" and a.confirmdate<='" + enddate + "'");
				sqlwhere2.append(" and confirmdate<='" + enddate + "'");
			}
			sqlwhere.append(" and a.status =3 ");
			sqlwhere.append(" and a.shopid=" + logid);
			sqlwhere2.append(" and status =3 ");
			sqlwhere2.append(" and shopid=" + logid);
			int i_pageNo = 1;
			if (pageNo != null && pageNo.trim().length() > 0) {
				i_pageNo = Integer.parseInt(pageNo);
			} else {
				i_pageNo = 1;
			}
			int i_pageSize = 10;
			if (pageSize != null && pageSize.trim().length() > 0) {
				i_pageSize = Integer.parseInt(pageSize);
			} else {
				i_pageSize = 10;
			}
			PageList list = this.getDdOrderDAO().findAll(sqlwhere.toString(),
					i_pageNo, i_pageSize);
			float sumamount = this.getDdOrderDAO().findSumAll(
					sqlwhere2.toString());
			sbf.append("<ordershops total=\"" + list.getTotalCount()
					+ "\" sum=\"" + sumamount + "\">");
			for (int i = 0; i < list.size(); i++) {
				Object[] obj = (Object[]) list.get(i);
				DdOrder du = (DdOrder) obj[0];
				DdShop ds = (DdShop) obj[1];
				String date2 = "";
				String content = "";
				if (du.getType().intValue() == 2) {
					// 定位
					if (ds.getType().intValue() == 1
							|| ds.getType().intValue() == 2
							|| ds.getType().intValue() == 3) {// 餐厅.娱乐,酒店
						DdUserorder1 duo = (DdUserorder1) this
								.getDdUserorder1DAO().findByOrderid(du.getId())
								.get(0);
						date2 = DateStrFormat.getCurrentTime(duo
								.getOrderdate1());
						content = duo.getName()
								+ (duo.getSex() == 0 ? "小姐" : "先生") + ",电话:"
								+ duo.getPhone();
						content = content + " 定金:" + du.getPrice()
								+ "元.预定消费日期 " + date2;
					} else if (ds.getType().intValue() == 5) {// 运动
						DdUserorder1 duo = (DdUserorder1) this
								.getDdUserorder1DAO().findByOrderid(du.getId())
								.get(0);
						date2 = DateStrFormat.getCurrentTime(duo
								.getOrderdate1());
						content = duo.getName()
								+ (duo.getSex() == 0 ? "小姐" : "先生") + ",电话:"
								+ duo.getPhone();
						content = content
								+ " 预定金:"
								+ du.getPrice()
								+ ".预定时间为:"
								+ DateStrFormat.getCurrentTime1(duo
										.getOrderdate1()) + " "
								+ duo.getFromto();
					}

				} else {
					DdUserorder duo = (DdUserorder) this.getDdUserorderDAO()
							.findByOrderid(du.getId()).get(0);
					DdShopgoods dsg = this.getDdGoodDAO().findById(
							Integer.parseInt(duo.getGoodslist()));
					date2 = duo.getSenddate();
					content = duo.getName2()
							+ (duo.getSex2() == 0 ? "小姐" : "先生") + ",电话:"
							+ duo.getPhone2();
					String sizestr = "";
					if (dsg.getStyleType().intValue() == 138) {
						sizestr = duo.getSize() + "寸";
					}
					content = content + "预定了贵店的 " + dsg.getName() + sizestr
							+ ".价格:" + du.getPrice();
				}

				sbf.append("<ordershop  shopid=\"" + ds.getId()
						+ "\" shopname=\"" + ds.getName() + "\" id=\""
						+ du.getId() + "\"  orderid=\""
						+ (du.getId() + 100000000) + "\"  orderdate=\""
						+ DateStrFormat.getCurrentTime(du.getOrderdate())
						+ " \" orderdate2=\"" + date2 + "\" orderstatus=\""
						+ getOrderstatus(du.getStatus()) + " \" type=\""
						+ du.getType() + "\" shoptype=\"" + ds.getType()
						+ "\" typestr=\""
						+ getOrderType(du.getType().intValue())
						+ "\" content=\"" + content + "\" score=\""
						+ du.getScore() + "\" />");
			}
			sbf.append("</ordershops></root>");
			out.print(sbf.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return null;
	}

	/**
	 * 查询订单
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward queryorder(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		Integer logid = (Integer) request.getSession()
				.getAttribute("shoplogid");// 网站用户是否已经登录

		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		if (logid == null) {// 商家用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
			return null;
		}
		try {
			String stratdate = request.getParameter("stratdate");// 订单创建的开始时间
			String enddate = request.getParameter("enddate");// 订单创建的结束时间
			String pageNo = request.getParameter("pageno");// 第几页
			String pageSize = request.getParameter("pagesize");// 每页记录数
			String status = request.getParameter("status");// 订单状态
			// String shopname = request.getParameter("shopname");// 商家名称
			String orderid = request.getParameter("orderid");// 商家名称
			StringBuffer sqlwhere = new StringBuffer();
			if (stratdate != null && stratdate.trim().length() > 0) {
				sqlwhere.append(" and a.orderdate>='" + stratdate + "'");
			}
			if (enddate != null && enddate.trim().length() > 0) {
				sqlwhere.append(" and a.orderdate<='" + enddate + "'");
			}

			if (status != null && status.trim().length() > 0) {
				sqlwhere.append(" and a.status in(" + status.trim() + ")");

			}
			if (orderid != null && orderid.trim().length() > 0) {
				long _orderid = 0;
				try {
					_orderid = Long.parseLong(orderid);
					_orderid = _orderid - 100000000;
				} catch (Exception e) {
					_orderid = 0;
				}
				sqlwhere.append(" and a.id =" + _orderid);

			}
			// if (shopname != null && shopname.trim().length() > 0) {
			// sqlwhere.append(" and b.name>='" + shopname + "'");
			// }
			sqlwhere.append(" and a.shopid=" + logid);
			int i_pageNo = 1;
			if (pageNo != null && pageNo.trim().length() > 0) {
				i_pageNo = Integer.parseInt(pageNo);
			} else {
				i_pageNo = 1;
			}
			int i_pageSize = 10;
			if (pageSize != null && pageSize.trim().length() > 0) {
				i_pageSize = Integer.parseInt(pageSize);
			} else {
				i_pageSize = 10;
			}
			PageList list = this.getDdOrderDAO().findAll(sqlwhere.toString(),
					i_pageNo, i_pageSize);
			sbf.append("<ordershops total=\"" + list.getTotalCount() + "\">");
			for (int i = 0; i < list.size(); i++) {
				Object[] obj = (Object[]) list.get(i);
				DdOrder du = (DdOrder) obj[0];
				DdShop ds = (DdShop) obj[1];
				String date2 = "";
				String content = "";
				if (du.getType().intValue() == 2) {
					// 定位
					if (ds.getType().intValue() == 1) {// 餐厅
						DdUserorder1 duo = (DdUserorder1) this
								.getDdUserorder1DAO().findByOrderid(du.getId())
								.get(0);
						date2 = DateStrFormat.getCurrentTime(duo
								.getOrderdate1());
						content = duo.getName()
								+ (duo.getSex() == 0 ? "小姐" : "先生") + ",电话:"
								+ duo.getPhone();
						content = content + " 一行约 " + duo.getPeople()
								+ " 人预定到贵店消费,已交定金" + du.getPrice()
								+ "元.-br-大约于 " + date2 + " 到达,位置要求:"
								+ duo.getLoge();
					} else if (ds.getType().intValue() == 5) {// 运动
						DdUserorder1 duo = (DdUserorder1) this
								.getDdUserorder1DAO().findByOrderid(du.getId())
								.get(0);
						date2 = DateStrFormat.getCurrentTime(duo
								.getOrderdate1());
						content = duo.getName()
								+ (duo.getSex() == 0 ? "小姐" : "先生") + ",电话:"
								+ duo.getPhone();
						content = content
								+ " 预定到贵场地运动.-br-时间为:"
								+ DateStrFormat.getCurrentTime1(duo
										.getOrderdate1()) + " "
								+ duo.getFromto() + ",已交定金" + du.getPrice();
					} else if (ds.getType().intValue() == 2) {// 娱乐
						DdUserorder1 duo = (DdUserorder1) this
								.getDdUserorder1DAO().findByOrderid(du.getId())
								.get(0);
						date2 = DateStrFormat.getCurrentTime(duo
								.getOrderdate1());
						content = duo.getName()
								+ (duo.getSex() == 0 ? "小姐" : "先生") + ",电话:"
								+ duo.getPhone();
						content = content + " 一行约 " + duo.getPeople()
								+ " 人预定到贵店消费,已交定金" + du.getPrice()
								+ "元.-br-大约于 " + date2 + " 到达,位置要求:"
								+ duo.getLoge();
					}

				} else {
					DdUserorder duo = (DdUserorder) this.getDdUserorderDAO()
							.findByOrderid(du.getId()).get(0);
					DdShopgoods dsg = this.getDdGoodDAO().findById(
							Integer.parseInt(duo.getGoodslist()));
					String sizestr = "";
					if (dsg.getStyleType().intValue() == 138) {
						sizestr = duo.getSize() + "寸";
					}
					date2 = duo.getSenddate();
					content = duo.getName2()
							+ (duo.getSex2() == 0 ? "小姐" : "先生") + ",电话:"
							+ duo.getPhone2();
					content = content + "预定了贵店的 " + dsg.getName() + sizestr
							+ ".-br-要求于 " + date2 + " 送到:" + duo.getAddress();
				}

				sbf.append("<ordershop  shopid=\"" + ds.getId()
						+ "\" shopname=\"" + ds.getName() + "\" id=\""
						+ du.getId() + "\"  orderid=\""
						+ (du.getId() + 100000000) + "\"  orderdate=\""
						+ DateStrFormat.getCurrentTime(du.getOrderdate())
						+ " \" orderdate2=\"" + date2 + "\" orderstatus=\""
						+ getOrderstatus(du.getStatus()) + " \" type=\""
						+ du.getType() + "\" shoptype=\"" + ds.getType()
						+ "\" typestr=\""
						+ getOrderType(du.getType().intValue())
						+ "\" content=\"" + content + "\" />");
			}
			sbf.append("</ordershops></root>");
			out.print(sbf.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return null;
	}

	/**
	 * 查询订单
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward systemqueryorder(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		String logid = String.valueOf((Integer) request.getSession()
				.getAttribute("systemlogid"));// 系统用户是否已经登录

		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		if (logid == null || logid.trim().length() == 0) {// 商家用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
			return null;
		}
		try {
			String stratdate = request.getParameter("stratdate");// 订单创建的开始时间
			String enddate = request.getParameter("enddate");// 订单创建的结束时间
			String pageNo = request.getParameter("pageno");// 第几页
			String pageSize = request.getParameter("pagesize");// 每页记录数
			String status = request.getParameter("status");// 订单状态
			String shopname = request.getParameter("shopname");// 商家名称
			String orderid = request.getParameter("orderid");// 商家名称
			String userid = request.getParameter("userid");// 商家名称
			String ulogid = request.getParameter("logid");// 商家名称
			StringBuffer sqlwhere = new StringBuffer();
			if (ulogid != null && ulogid.trim().length() > 0) {
				DdUser du = this.getDdUserDAO().findBylogid(ulogid);
				if (du == null) {
					ulogid = "0";
				} else {
					ulogid = du.getId() + "";
				}
				sqlwhere.append(" and a.userid =" + ulogid);
			}
			if (stratdate != null && stratdate.trim().length() > 0) {
				sqlwhere.append(" and a.orderdate>='" + stratdate + "'");
			}
			if (enddate != null && enddate.trim().length() > 0) {
				sqlwhere.append(" and a.orderdate<='" + enddate + "'");
			}
			if (status != null && status.trim().length() > 0) {
				sqlwhere.append(" and a.status in(" + status.trim() + ")");

			}
			if (shopname != null && shopname.trim().length() > 0) {
				sqlwhere.append(" and b.name like '%" + shopname + "%'");
			}
			if (userid != null && userid.trim().length() > 0) {
				sqlwhere.append(" and b.belong =" + userid);
			}
			if (orderid != null && orderid.trim().length() > 0) {
				long _orderid = 0;
				try {
					_orderid = Long.parseLong(orderid);
					_orderid = _orderid - 100000000;
				} catch (Exception e) {
					_orderid = 0;
				}
				sqlwhere.append(" and a.id =" + _orderid);

			}
			// if (shopname != null && shopname.trim().length() > 0) {
			// sqlwhere.append(" and b.name>='" + shopname + "'");
			// }
			// sqlwhere.append(" and a.shopid=" + logid);
			int i_pageNo = 1;
			if (pageNo != null && pageNo.trim().length() > 0) {
				i_pageNo = Integer.parseInt(pageNo);
			} else {
				i_pageNo = 1;
			}
			int i_pageSize = 10;
			if (pageSize != null && pageSize.trim().length() > 0) {
				i_pageSize = Integer.parseInt(pageSize);
			} else {
				i_pageSize = 10;
			}
			PageList list = this.getDdOrderDAO().findAll(sqlwhere.toString(),
					i_pageNo, i_pageSize);
			sbf.append("<ordershops total=\"" + list.getTotalCount() + "\">");
			for (int i = 0; i < list.size(); i++) {
				Object[] obj = (Object[]) list.get(i);
				DdOrder du = (DdOrder) obj[0];
				DdShop ds = (DdShop) obj[1];
				DdUser dduser = this.getDdUserDAO().findById(du.getUserid());
				String date2 = "";
				String content = "";
				long paydelay = 0;
				if (du.getPaydate() != null) {
					paydelay = System.currentTimeMillis()
							- du.getPaydate().getTime();
				}
				if (du.getType().intValue() == 2) {
					// 定位
					if (ds.getType().intValue() == 1) {// 餐厅
						DdUserorder1 duo = (DdUserorder1) this
								.getDdUserorder1DAO().findByOrderid(du.getId())
								.get(0);
						date2 = DateStrFormat.getCurrentTime(duo
								.getOrderdate1());
						content = duo.getName()
								+ (duo.getSex() == 0 ? "小姐" : "先生") + ",电话:"
								+ duo.getPhone();
						content = content + " 一行约 " + duo.getPeople()
								+ " 人预定到贵店消费,已交定金" + du.getPrice()
								+ "元.-br-大约于 " + date2 + " 到达,位置要求:"
								+ duo.getLoge();
					} else if (ds.getType().intValue() == 5) {// 运动
						DdUserorder1 duo = (DdUserorder1) this
								.getDdUserorder1DAO().findByOrderid(du.getId())
								.get(0);
						date2 = DateStrFormat.getCurrentTime(duo
								.getOrderdate1());
						content = duo.getName()
								+ (duo.getSex() == 0 ? "小姐" : "先生") + ",电话:"
								+ duo.getPhone();
						content = content
								+ " 预定到贵场地运动.-br-时间为:"
								+ DateStrFormat.getCurrentTime1(duo
										.getOrderdate1()) + " "
								+ duo.getFromto() + ",已交定金" + du.getPrice();
					} else if (ds.getType().intValue() == 2) {// 娱乐
						DdUserorder1 duo = (DdUserorder1) this
								.getDdUserorder1DAO().findByOrderid(du.getId())
								.get(0);
						date2 = DateStrFormat.getCurrentTime(duo
								.getOrderdate1());
						content = duo.getName()
								+ (duo.getSex() == 0 ? "小姐" : "先生") + ",电话:"
								+ duo.getPhone();
						content = content + " 一行约 " + duo.getPeople()
								+ " 人预定到贵店消费,已交定金" + du.getPrice()
								+ "元.-br-大约于 " + date2 + " 到达,位置要求:"
								+ duo.getLoge();
					}

				} else {
					DdUserorder duo = (DdUserorder) this.getDdUserorderDAO()
							.findByOrderid(du.getId()).get(0);
					DdShopgoods dsg = this.getDdGoodDAO().findById(
							Integer.parseInt(duo.getGoodslist()));
					date2 = duo.getSenddate();
					content = duo.getName2()
							+ (duo.getSex2() == 0 ? "小姐" : "先生") + ",电话:"
							+ duo.getPhone2();
					content = content + "预定了贵店的 " + dsg.getName() + ".-br-要求于 "
							+ date2 + " 送到:" + duo.getAddress();
				}
				String tem="";
				if(du.getCode()==null){
					tem=" 未付款";
				}
				int count1 =  this.getDdOrderDAO().findCountByUid(dduser.getId());
				int count2 =  this.getDdOrderDAO().findCountByUid2(dduser.getId());
				sbf.append("<ordershop countstr=\""+count1+"-"+(count2+count1)+"\" logid=\"" + dduser.getLogid()
						+ "\" shopid=\"" + ds.getId() + "\" shopname=\""
						+ ds.getName() + "\" shoptype=\"" + ds.getType()
						+ "\" id=\"" + du.getId() + "\"  orderid=\""
						+ (du.getId() + 100000000) + "\"  orderdate=\""
						+ DateStrFormat.getCurrentTime(du.getOrderdate())
						+ " \" orderdate2=\"" + date2 + "\" orderstatus=\""
						+ du.getStatus() + " \" orderstatusstr=\""
						+ getOrderstatus(du.getStatus())+tem + " \"  type=\""
						+ du.getType() + "\"  typestr=\""
						+ getOrderType(du.getType().intValue())
						+ "\" content=\"" + content + "\" paydelay=\""
						+ paydelay / 1000 + "\" />");
			}
			sbf.append("</ordershops></root>");
			out.print(sbf.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return null;
	}

	private String getOrderType(int type) {
		String typestr = type == 1 ? "订礼品" : type == 2 ? "订位" : "订礼品";
		return typestr;
	}

	private String getOrderstatus(int type) {
		// 订单状态 0创建 1用户确认 2商家确认 3完成 -1 用户取消 -2商家取消-3客户取消
		String typestr = type == 0 ? "创建" : type == 1 ? "用户已付款"
				: type == 2 ? "商家已确认" : type == 3 ? "完成" : type == -1 ? "用户取消"
						: type == -2 ? "商家取消" : "客服取消";
		return typestr;
	}

	/**
	 * 查询订单明细
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward orderinfo(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		String logid = String.valueOf((Integer) request.getSession()
				.getAttribute("shoplogid"));// 网站用户是否已经登录
		StringBuffer sbf = new StringBuffer(
				"<?xml version=\"1.0\" encoding=\"utf-8\"?><root>");
		if (logid == null || logid.trim().length() == 0) {// 网站用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
			return null;
		}
		try {
			String orderid = request.getParameter("orderid");

			List list = this.getDdOrderDAO().find(Integer.parseInt(orderid));
			if (list == null || list.size() == 0) {// 订单不存在
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>2</error></root>");
				return null;
			}
			// if()
			Object[] obj = (Object[]) list.get(0);
			DdOrder ddorder = (DdOrder) obj[0];
			DdShop ddShop = (DdShop) obj[1];
			if (ddorder.getType() == 1) {// 订餐、订实物
				DdUserorder du = (DdUserorder) this.getDdUserorderDAO()
						.findByOrderid(ddorder.getId()).get(0);
				String sex = du.getSex().intValue() == 0 ? "女" : "男";
				int goodid = Integer.parseInt(du.getGoodslist());// 礼物只传ID
				DdShopgoods ddShopgoods = this.getDdGoodDAO().findById(goodid);
				sbf.append("<orderid>" + (du.getId() + 100000000)
						+ "</orderid><ordergoodslist>" + ddShopgoods.getName()
						+ "</ordergoodslist><orderaddress>" + du.getAddress()
						+ "</orderaddress><orderpeoplename>" + du.getName()
						+ "</orderpeoplename><orderpeoplesex>" + sex
						+ "</orderpeoplesex><orderpeoplephone>" + du.getPhone()
						+ "</orderpeoplephone><orderdate>"
						+ DateStrFormat.getCurrentTime(du.getOrderdate())
						+ "</orderdate>" + "<orderstatus>"
						+ getOrderstatus(ddorder.getStatus())
						+ "</orderstatus>");
			} else if (ddorder.getType() == 2) {// 订位置
				DdUserorder1 du = (DdUserorder1) this.getDdUserorder1DAO()
						.findByOrderid(ddorder.getId()).get(0);
				String sex = du.getSex().intValue() == 0 ? "女" : "男";
				sbf.append("<shopname>" + ddShop.getName() + "</shopname>"
						+ "<orderid>" + du.getId()
						+ "</orderid> <orderpeoplename>" + du.getName()
						+ "</orderpeoplename> <orderpeoplesex>" + sex
						+ "</orderpeoplesex> <orderpeoplephone>"
						+ du.getPhone() + "</orderpeoplephone>  <orderdate>"
						+ DateStrFormat.getCurrentTime(du.getOrderdate())
						+ "</orderdate>  <orderstatus>"
						+ getOrderstatus(ddorder.getStatus())
						+ "</orderstatus> <orderdate1>"
						+ DateStrFormat.getCurrentTime(du.getOrderdate1())
						+ "</orderdate1> <orderloge>" + du.getLoge()
						+ "</orderloge><orderpeople>" + du.getPeople()
						+ "</orderpeople>");
			}
			sbf.append("</root>");
			out.print(sbf.toString());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return null;
	}

	/**
	 * 修改订单状态
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward updateorder(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		Integer logid = (Integer) request.getSession()
				.getAttribute("shoplogid");// 网站用户是否已经登录
		if (logid == null) {// 网站用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
			return null;
		}

		try {
			String id = request.getParameter("id");// 订单ID
			// 订单状态 0等待 1用户确认 2商家确认 3完成 -1 用户取消 -2商家取消-3客户取消
			String status = request.getParameter("status");
			if (id != null && id.trim().length() > 0) {
				DdOrder ddorder = this.getDdOrderDAO().findById(
						Integer.parseInt(id));
				DdShop ds = this.getDdShopDAO().findById(ddorder.getShopid());
				String phone = "";
				String succstr = "";
				String succstr2 = "";
				String name = "";
				if (ddorder.getType().intValue() == 2) {
					// 定位
					DdUserorder1 duo = (DdUserorder1) this.getDdUserorder1DAO()
							.findByOrderid(ddorder.getId()).get(0);
					phone = duo.getPhone();
					name = duo.getName() + (duo.getSex() == 0 ? "小姐" : "先生");
					if (ds.getType().intValue() == 1) {// 餐厅
						succstr = ",请向商家索取确认码换积分。(享受优惠不开 票)";
						succstr2 = ",请您安排位置。消费确认码为" + ddorder.getCode() + ".";
					} else if (ds.getType().intValue() == 2) {// 娱乐 
						succstr = ",请向商家索取确认码换积分。(享受优惠不开 票)";
						succstr2 = ",请您安排位置。消费确认码为" + ddorder.getCode() + ".";
					} else if (ds.getType().intValue() == 3) {// 酒店
						succstr = ",请向商家索取确认码换积分。(享受优惠不开 票)";
						succstr2 = ",请您安排位置。消费确认码为" + ddorder.getCode() + ".";
					} else if (ds.getType().intValue() == 5) {// 运动
						succstr = ",消费完成后请登陆蛋订网确认消费换取积分。";
						succstr2 = ",请您安排位置。";
					}

				} else {
					DdUserorder duo = (DdUserorder) this.getDdUserorderDAO()
							.findByOrderid(ddorder.getId()).get(0);
					phone = duo.getPhone2();
					name = duo.getName2() + (duo.getSex2() == 0 ? "小姐" : "先生");
					succstr = ",我们会尽快将商品送达，确认收到商品后请登陆蛋订网评分，以换取积分.";
					succstr2 = ",请按客户要求时间派送.";
				}
				if (ddorder == null) {// 订单不存在
					out
							.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>2</result></root>");
					return null;
				}
				// 定位订单商家拒绝.插入拒绝表.下次该时间点就不能下订单.只精确到小时
				if (status.equals("-2") && ddorder.getType().intValue() == 2) {
					DdUserorder1 duo = (DdUserorder1) this.getDdUserorder1DAO()
							.findByOrderid(ddorder.getId()).get(0);
					String date2 = DateStrFormat.getCurrentTimeForReject(duo
							.getOrderdate1());
					DdShopreject dsj = new DdShopreject();
					dsj.setShopid(ddorder.getShopid());
					dsj.setRejectdate(date2);
					int loge_i = 0;
					if (duo.getLoge()!=null&&duo.getLoge().equals("只订包厢")) {
						loge_i = 1;
					}
					dsj.setLoge(loge_i);
					this.getDdShoprejectDAO().saveOrUpdate(dsj);
				}
				if (status.equals("-2") || status.equals("-3")) {
					// 取消订单,退款
					DdUserpay up = this.getDdUserpayDAO().findByOrderid(
							ddorder.getId(), 2);
					DdUserpay up_tmp = this.getDdUserpayDAO().findByOrderid(
							ddorder.getId(), -2);
					if (up_tmp == null) { // 未退款
						DdUser du = this.getDdUserDAO()
								.findById(up.getUserid());
						du.setAmount(du.getAmount() + up.getAmount());
						du.setAmountfree(du.getAmountfree()
								+ up.getAmountfree());
						this.getDdUserDAO().saveOrUpdate(du);
						// 新建个退款的userpay,直接把定金原路返回到user中,真实币与虚拟币分开计算
						DdUserpay up1 = new DdUserpay();
						up1.setUserid(du.getId());
						up1.setOrderid(ddorder.getId());
						up1.setPaydate(new Date());
						up1.setPaytype(-2);
						up1.setStatus(2);
						up1.setAmount(up.getAmount());
						up1.setAmountfree(up.getAmountfree());
						up1.setShopid(up.getShopid());
						up1.setRemark(up.getRemark());
						this.getDdUserpayDAO().saveOrUpdate(up1);
					}
					// 添加短信通知 to 客户 ,订单被取消

					DdSms dsms = new DdSms();
					dsms.setSendmsg("【蛋订网】" + name + "您好,您的订单:"
							+ (ddorder.getId() + 100000000 + "")
							+ " 被商家取消,订金已退回到您的蛋订币，给您带来的不便请见谅。");
					dsms.setOrderid(ddorder.getId());
					dsms.setOrderstatus(Integer.parseInt(status));
					dsms.setTypes(2);
					dsms.setStatus(0);
					dsms.setPhone(phone);
					this.getDdSmsDAO().saveOrUpdate(dsms);
				}
				if (status.equals("2")) {
					// 确认订单
					// 添加短信通知
					// 客户:订单被确认+订单号
					// 商家:订单号+用户消费码+用户确认码

					DdSms dsms = new DdSms();
					dsms.setSendmsg("【蛋订网】" + name + "您的订单:"
							+ (ddorder.getId() + 100000000 + "") + " 商家已经确认"
							+ succstr);
					dsms.setOrderid(ddorder.getId());
					dsms.setOrderstatus(Integer.parseInt(status));
					dsms.setTypes(2);
					dsms.setStatus(0);
					dsms.setPhone(phone);
					this.getDdSmsDAO().saveOrUpdate(dsms);
					dsms = new DdSms();
					dsms.setSendmsg("【蛋订网】订单号:"
							+ (ddorder.getId() + 100000000 + "") + " 您已确认"
							+ succstr2);
					dsms.setOrderid(ddorder.getId());
					dsms.setOrderstatus(Integer.parseInt(status));
					dsms.setTypes(2);
					dsms.setStatus(0);
					dsms.setPhone(ds.getMobile());
					this.getDdSmsDAO().saveOrUpdate(dsms);
				}
				ddorder.setStatus(Integer.parseInt(status));
				this.getDdOrderDAO().save(ddorder);
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>1</result></root>");
			} else {
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>0</result></root>");

			}
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 修改订单状态
	 * 
	 * @param mapping
	 * @param pcUserForm
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public ActionForward systemupdateorder(ActionMapping mapping,
			ActionForm pcUserForm, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		// HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		Integer logid = (Integer) request.getSession().getAttribute(
				"systemlogid");// 网站用户是否已经登录
		if (logid == null) {// 网站用户尚未登录
			// 返回-1
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>-1</error></root>");
			return null;
		}

		try {
			String id = request.getParameter("id");// 订单ID
			// 订单状态 0等待 1用户确认 2商家确认 3完成 -1 用户取消 -2商家取消-3客户取消
			String status = request.getParameter("status");
			if (id != null && id.trim().length() > 0) {
				DdOrder ddorder = this.getDdOrderDAO().findById(
						Integer.parseInt(id));
				DdShop ds = this.getDdShopDAO().findById(ddorder.getShopid());
				String phone = "";
				String succstr = "";
				String succstr2 = "";
				String name = "";
				if (ddorder.getType().intValue() == 2) {
					// 定位
					DdUserorder1 duo = (DdUserorder1) this.getDdUserorder1DAO()
							.findByOrderid(ddorder.getId()).get(0);
					phone = duo.getPhone();
					name = duo.getName() + (duo.getSex() == 0 ? "小姐" : "先生");
					if (ds.getType().intValue() == 1) {// 餐厅
						succstr = ",请向商家索取确认码换积分。(享受优惠不开 票)";
						succstr2 = ",请您安排位置。消费确认码为" + ddorder.getCode() + ".";
					} else if (ds.getType().intValue() == 2) {// 娱乐
						succstr = ",请向商家索取确认码换积分。(享受优惠不开 票)";
						succstr2 = ",请您安排位置。消费确认码为" + ddorder.getCode() + ".";
					} else if (ds.getType().intValue() == 3) {// 酒店
						succstr = ",请向商家索取确认码换积分。(享受优惠不开 票)";
						succstr2 = ",请您安排位置。消费确认码为" + ddorder.getCode() + ".";
					} else if (ds.getType().intValue() == 5) {// 运动
						succstr = ",请向商家索取确认码换积分。(享受优惠不开 票)";
						succstr2 = ",请您安排位置。";
					}

				} else {
					DdUserorder duo = (DdUserorder) this.getDdUserorderDAO()
							.findByOrderid(ddorder.getId()).get(0);
					phone = duo.getPhone2();
					name = duo.getName2() + (duo.getSex2() == 0 ? "小姐" : "先生");
					succstr = ",我们会尽快将商品送达，确认收到商品后请登陆蛋订网评分，以换取积分.";
					succstr2 = ",请按客户要求时间派送.";
				}
				if (ddorder == null) {// 订单不存在
					out
							.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>2</result></root>");
					return null;
				}
				// 定位订单商家拒绝.插入拒绝表.下次该时间点就不能下订单.只精确到小时
				if (status.equals("-2") && ddorder.getType().intValue() == 2) {
					DdUserorder1 duo = (DdUserorder1) this.getDdUserorder1DAO()
							.findByOrderid(ddorder.getId()).get(0);
					String date2 = DateStrFormat.getCurrentTimeForReject(duo
							.getOrderdate1());
					DdShopreject dsj = new DdShopreject();
					dsj.setShopid(ddorder.getShopid());
					dsj.setRejectdate(date2);
					int loge_i = 0;
					if (duo.getLoge()!=null&&duo.getLoge().equals("只订包厢")) {
						loge_i = 1;
					}
					dsj.setLoge(loge_i);
					this.getDdShoprejectDAO().saveOrUpdate(dsj);
				}
				if (status.equals("-2") || status.equals("-3")) {
					// 取消订单,退款
					DdUserpay up = this.getDdUserpayDAO().findByOrderid(
							ddorder.getId(), 2);
					if(up!=null){//不要订金的商家
						DdUserpay up_tmp = this.getDdUserpayDAO().findByOrderid(
								ddorder.getId(), -2);
						if (up_tmp == null) { // 未退款
							DdUser du = this.getDdUserDAO()
									.findById(up.getUserid());
							du.setAmount(du.getAmount() + up.getAmount());
							du.setAmountfree(du.getAmountfree()
									+ up.getAmountfree());
							this.getDdUserDAO().saveOrUpdate(du);
							// 新建个退款的userpay,直接把定金原路返回到user中,真实币与虚拟币分开计算
							DdUserpay up1 = new DdUserpay();
							up1.setUserid(du.getId());
							up1.setOrderid(ddorder.getId());
							up1.setPaydate(new Date());
							up1.setPaytype(-2);
							up1.setStatus(2);
							up1.setAmount(up.getAmount());
							up1.setAmountfree(up.getAmountfree());
							up1.setShopid(up.getShopid());
							up1.setRemark(up.getRemark());
							this.getDdUserpayDAO().saveOrUpdate(up1);
						}
					}
					
					// 添加短信通知 to 客户 ,订单被取消

					DdSms dsms = new DdSms();
					dsms.setSendmsg("【蛋订网】" + name + "您好,您的订单:"
							+ (ddorder.getId() + 100000000 + "")
							+ " 被商家取消,订金已退回到您的蛋订币，给您带来的不便请见谅。");
					dsms.setOrderid(ddorder.getId());
					dsms.setOrderstatus(Integer.parseInt(status));
					dsms.setTypes(2);
					dsms.setStatus(0);
					dsms.setPhone(phone);
					this.getDdSmsDAO().saveOrUpdate(dsms);
				}
				if (status.equals("2")) {
					// 确认订单
					// 添加短信通知
					// 客户:订单被确认+订单号
					// 商家:订单号+用户消费码+用户确认码

					DdSms dsms = new DdSms();
					dsms.setSendmsg("【蛋订网】" + name + "您的订单:"
							+ (ddorder.getId() + 100000000 + "") + " 商家已经确认"
							+ succstr);
					dsms.setOrderid(ddorder.getId());
					dsms.setOrderstatus(Integer.parseInt(status));
					dsms.setTypes(2);
					dsms.setStatus(0);
					dsms.setPhone(phone);
					this.getDdSmsDAO().saveOrUpdate(dsms);
					dsms = new DdSms();
					dsms.setSendmsg("【蛋订网】订单号:"
							+ (ddorder.getId() + 100000000 + "") + " 您已确认"
							+ succstr2);
					dsms.setOrderid(ddorder.getId());
					dsms.setOrderstatus(Integer.parseInt(status));
					dsms.setTypes(2);
					dsms.setStatus(0);
					dsms.setPhone(ds.getMobile());
					this.getDdSmsDAO().saveOrUpdate(dsms);
				}
				ddorder.setStatus(Integer.parseInt(status));
				this.getDdOrderDAO().save(ddorder);
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>1</result></root>");
			} else {
				out
						.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><result>0</result></root>");

			}
			return null;
		} catch (Exception e) {
			out
					.print("<?xml version=\"1.0\" encoding=\"utf-8\"?><root><error>0</error></root>");
			out.flush();
			out.close();
			e.printStackTrace();
			return null;
		}
	}
}
